//
//  UIViewController+StoryBoard.m
//  duzhoumo
//
//  Created by 度周末网络-王腾 on 15/11/10.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "UIViewController+StoryBoard.h"

@implementation UIViewController (StoryBoard)

+(UIViewController *)getInitialViewControllerFromSbName:(NSString *)sbName{
    UIViewController *viewController = [[UIStoryboard storyboardWithName:sbName bundle:nil]  instantiateInitialViewController];
    return viewController;
}

+(UIViewController *)getViewControllerFromSbName:(NSString *)sbName viewControllerName:(NSString *)vcName{
    UIViewController *viewController = [[UIStoryboard storyboardWithName:sbName bundle:nil]  instantiateViewControllerWithIdentifier:vcName];
    return viewController;
}


-(UIViewController *)isContaintViewConttoller:(NSString *)viewControllerName{
    for (UIViewController *controller in self.navigationController.viewControllers){
        if ([controller isKindOfClass:NSClassFromString(viewControllerName)]) {
            return controller;
        }
    }
    return nil;
}

-(void)backCustomViewController:(NSString *)viewControllerName isRoot:(BOOL)isRoot{
    
    BOOL successBack = NO;
    for (UIViewController *controller in self.navigationController.viewControllers){
        if ([controller isKindOfClass:NSClassFromString(viewControllerName)]) {
            successBack = YES;
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
    
    if (!successBack && isRoot) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else if (!successBack && !isRoot){
         [self.navigationController popViewControllerAnimated:YES];
    }
}


-(void)afterDelay:(NSTimeInterval )timerInterval block:(void (^)())block{
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timerInterval*NSEC_PER_SEC));
    dispatch_after(time, dispatch_get_main_queue(), ^{
        block();
    });
}

@end
